package com.acme.bankapp.domain.bank;

import com.acme.bankapp.exceptions.NotEnoughFundException;
import com.acme.bankapp.exceptions.OverDraftLimitExceededException;

public class CheckingAccount extends AbstractAccount {
    private double overdraft;

    private double overdraftLimit;
    public CheckingAccount(double balance, double overdraft) throws IllegalArgumentException {
        super(balance);

        if ( overdraft < 0 )
            throw new IllegalArgumentException();
        this.overdraft = overdraft;
        this.overdraftLimit = overdraft;
    }

    public double getOverdraft() {
        return overdraft;
    }

    @Override
    public double maximumAmountToWithdraw() {
        return ( balance + overdraft );
    }

    @Override
    public void withdraw(double amount) throws OverDraftLimitExceededException {
        if ( amount > overdraft + balance ) {
            throw new OverDraftLimitExceededException( overdraft + balance );
        }
        double temp = balance - amount;
        balance = Math.max(temp, 0);
        overdraft -= Math.min(temp, 0);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        if (!super.equals(o)) return false;

        CheckingAccount that = (CheckingAccount) o;

        if (Double.compare(that.overdraft, overdraft) != 0) return false;
        if (Double.compare(that.overdraftLimit, overdraftLimit) != 0) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = super.hashCode();
        long temp;
        temp = overdraft != +0.0d ? Double.doubleToLongBits(overdraft) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        temp = overdraftLimit != +0.0d ? Double.doubleToLongBits(overdraftLimit) : 0L;
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public String toString() {
        return new StringBuffer("CheckingAccount{")
                .append("overdraft=")
                .append(overdraft)
                .append(", overdraftLimit=")
                .append(overdraftLimit)
                .append('}')
                .toString();
    }
}